add_executable(abc-aop main.cpp)

if (WIN32)
    target_compile_definitions(abc-aop PRIVATE PLATFORM_WIN)
elseif (APPLE)
    target_compile_definitions(abc-aop PRIVATE PLATFORM_MAC)
    target_compile_definitions(abc-aop PRIVATE -fPIC)
endif()

set(FILE_PATH "${PROJECT_SOURCE_DIR}/abc/modules.abc")
set(OUT_FILE_PATH "${PROJECT_SOURCE_DIR}/abc/modules_new.abc")

target_compile_definitions(abc-aop
    PRIVATE 
        FILE_PATH="${FILE_PATH}"
        OUT_FILE_PATH="${OUT_FILE_PATH}"
)

target_include_directories(abc-aop PRIVATE ${INCLUDE_DIR})

target_link_libraries(abc-aop PRIVATE
    "${ABCKIT_LIB}"
    "${ARK_ASSEMBLER_LIB}"
)

if(WIN32)
    file(GLOB LIB_FILES "${LIB_DIR}/*.lib")
    message(STATUS "Linking library: ${LIB_FILES}")
    target_link_libraries(abc-aop PRIVATE ${LIB_FILES})

    file(GLOB DLL_FILES "${LIB_DIR}/*.dll")
    foreach(dll_file ${DLL_FILES})
        get_filename_component(dll_name ${dll_file} NAME)
        add_custom_command(TARGET abc-aop POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
            "${dll_file}"
            "$<TARGET_FILE_DIR:abc-aop>"
            COMMENT ""
        )        
    endforeach()
    # Also copy MinGW runtime DLLs that the executable depends on (libstdc++/libgcc/libwinpthread)
    get_filename_component(CXX_BIN_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)
    set(MINGW_RUNTIME_DLLS
        "${CXX_BIN_DIR}/libstdc++-6.dll"
        "${CXX_BIN_DIR}/libgcc_s_seh-1.dll"
        "${CXX_BIN_DIR}/libwinpthread-1.dll"
    )
    foreach(rt_dll ${MINGW_RUNTIME_DLLS})
        if(EXISTS ${rt_dll})
            add_custom_command(TARGET abc-aop POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${rt_dll}"
                "$<TARGET_FILE_DIR:abc-aop>"
                COMMENT ""
            )
        endif()
    endforeach()
    add_custom_command(TARGET abc-aop POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo 
        "Copied ${DLL_FILES} DLL files."
        COMMENT "Copying DLL dependencies"
    )
endif()